<?php
/**
 * LilFloat Behavior for CakePHP 2.0
 * 
 * @copyright     Copyright 2008, Miha Nahtigal (http://www.nahtigal.com)
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 * 
 * 
 * This LilDate behavior for CakePHP allows you to auto convert date values from local
 * format to default format suitable for saving into database
 * 
 */
class LilDateBehavior extends ModelBehavior {
    
/**
 * An array of settings set by the $actsAs property
 */
    private $_settings = array();

/**
 * Instantiates the behavior and sets the magic methods
 * 
 * @param object $model The Model object
 * @param array $settings Array of scope properties
 */
	public function setup($model, $settings = array()) {
		$this->_settings[$model->alias] = $settings;
	}

/**
 * Model callback function which converts local float values to international representation
 * 
 */
	public function beforeSave(&$model, $options) {
		$fields = $model->schema();
		App::uses('LilDateEngine', 'Lil.Lib');
		$lf = LilDateEngine::getInstance();
		foreach ($fields as $field_name => $field_data) {
			// convert local representation to date-time format
			if (($field_data['type'] == 'date' || $field_data['type'] == 'datetime') && 
				!empty($model->data[$model->alias][$field_name])) 
			{
				if ($lf->isValidDate($model->data[$model->alias][$field_name])) {
					$model->data[$model->alias][$field_name] = $lf->delocalize(
						$model->data[$model->alias][$field_name],
						$this->_settings[$model->alias]
					);
				}
				
				if (isset($model->data[$model->alias][$field_name . '_time']['hour'])) {
					$model->data[$model->alias][$field_name] .= ' ' . $model->data[$model->alias][$field_name . '_time']['hour'];
					
					if (isset($model->data[$model->alias][$field_name . '_time']['min'])) {
						$model->data[$model->alias][$field_name] .= ':' . $model->data[$model->alias][$field_name . '_time']['min'];
					} else {
						$model->data[$model->alias][$field_name] .= ':00'; 
					}
					unset($model->data[$model->alias][$field_name . '_time']);
				}
			}
			
			if ($field_data['type'] == 'integer' && !isset($model->data[$model->alias][$field_name])) {
				$duration = null; $subfieldExist = false;
				
				if (isset($model->data[$model->alias][$field_name . '_minutes'])) {
					$subfieldExist = true;
					if (trim($model->data[$model->alias][$field_name . '_minutes']) != '')
						$duration += $model->data[$model->alias][$field_name . '_minutes'] * 60;
				}
				
				if (isset($model->data[$model->alias][$field_name . '_seconds'])) {
					$subfieldExist = true;
					if (trim($model->data[$model->alias][$field_name . '_seconds']) != '')
						$duration += $model->data[$model->alias][$field_name . '_seconds'];
				}
				
				if (isset($model->data[$model->alias][$field_name . '_hours'])) {
					$subfieldExist = true;
					if (trim($model->data[$model->alias][$field_name . '_hours']) != '')
						$duration += $model->data[$model->alias][$field_name . '_hours'] * 60 * 60;
				}
				
				if (isset($model->data[$model->alias][$field_name . '_days'])) {
					$subfieldExist = true;
					if (trim($model->data[$model->alias][$field_name . '_days']) != '')
						$duration += $model->data[$model->alias][$field_name . '_days'] * 24 * 60 * 60;
				}
				
				if ($subfieldExist) $model->data[$model->alias][$field_name] = $duration;
					
			}
		}
		
		return true;
	}
/**
 * Sets new date separator
 *
 * @param string $dateSeparator New date separator
 * @access public
 */
	public function setDateSeparator(&$model, $dateSeparator = null) {
		$this->_settings[$model->alias]['dateSeparator'] = $decimalSeparator;
	}

/**
 * Sets new date format
 *
 * @param string $dateFormat New date format (YMD, DMY,..)
 * @access public
 */
	public function setDateFormat(&$model, $dateFormat = null) {
		$this->_settings[$model->alias]['dateFormat'] = $dateFormat;
	}
	
/**
 * Returns date to SQL form YYYY-MM-DD
 *
 * @param string $localDate Local date
 * @access public
 */
	public function delocalize(&$model, $localDate) {
		App::uses('LilDateEngine', 'Lil.Lib');
		$lf = LilDateEngine::getInstance();
		return $lf->delocalize($localDate, $this->_settings[$model->alias]);
	}
    
/**
 * Checks that a value is a valid date in local format. 
 *
 * @param array $data Field-value array pair to check
 * @return boolean Success
 * @access public
 */
	public function isValidDate(&$model, $data) {
		App::uses('LilDateEngine', 'Lil.Lib');
		$lf = LilDateEngine::getInstance();
		return $lf->isValidDate(reset($data), $this->_settings[$model->alias]);
	}

}